//
//  MeViewController.m
//  oschina
//
//  Created by 如佳 陈 on 15/5/30.
//  Copyright (c) 2015年 crj. All rights reserved.
//

#import "MeViewController.h"

@interface MeViewController ()<UITableViewDataSource, UITableViewDelegate>{
    UIImageView *_portrait;
}

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *userBgView;
@property (nonatomic, retain) NSArray *items;

@end


static NSString *identifier = @"user_cell";
@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIImageView *)userBgView{
    if (!_userBgView) {
        _userBgView = [[UIImageView alloc]init];
        _userBgView.frame = CGRectMake(0, 0, 375, 160);
//        _userBgView.translatesAutoresizingMaskIntoConstraints = NO;
        _userBgView.image = [UIImage imageNamed:@"user-background-375"];
        
        UIView *portraitBG = [[UIView alloc]init];
        portraitBG.backgroundColor = [UIColor whiteColor];
        portraitBG.translatesAutoresizingMaskIntoConstraints = NO;
        [portraitBG setCornerRadius:27];
        [_userBgView addSubview:portraitBG];
        
        _portrait = [[UIImageView alloc]init];
        _portrait.image = [UIImage imageNamed:@"default－portrait"];
        _portrait.translatesAutoresizingMaskIntoConstraints = NO;
        _portrait.bounds = CGRectMake(0, 0, 50, 50);
        [_portrait setCornerRadius:25];
        [_userBgView addSubview:_portrait];
        NSDictionary *views = NSDictionaryOfVariableBindings(_userBgView, _portrait,portraitBG);
        [_userBgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_portrait(50)]" options:0 metrics:nil views:views]];
        [_userBgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_portrait(50)]" options:0 metrics:nil views:views]];
        
        [_userBgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[portraitBG(54)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
        [_userBgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[portraitBG(54)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
        
        [_userBgView addConstraint:[NSLayoutConstraint constraintWithItem:_portrait attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:portraitBG attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
        [_userBgView addConstraint:[NSLayoutConstraint constraintWithItem:_portrait attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:portraitBG attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];

        [_userBgView addConstraint:[NSLayoutConstraint constraintWithItem:_userBgView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:portraitBG attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    }
    return _userBgView;
}

- (NSArray *)items{
    if (!_items) {
        _items = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"MeViewControllerConfig" ofType:@"plist"]];
    }
    return _items;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [Util setUpTableView:_tableView];
    }
    return _tableView;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.userBgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return self.userBgView.frame.size.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.items.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [Util setUpTableViewCell:cell];
    cell.textLabel.text = self.items[indexPath.row][@"Desc"];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"me-%@",self.items[indexPath.row][@"Name"]]];
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
